//
//  PhotoViewController.swift
//  1604_LimitFree
//
//  Created by 千锋 on 16/9/27.
//  Copyright © 2016年 Mabin. All rights reserved.
//

import UIKit



class PhotoViewController: UIViewController {

    //点击图片的序号
    var photoindex : Int?
    
    //所有图片的网址信息
    var photoArray : Array<PhotoModel>?
    
    //标题文字
    private var titleLabel : UILabel?
    
    //滚动视图
    private var scrollView : UIScrollView?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.darkGrayColor()
        
        //导航
        createMyNav()
        
        
//        //进入下载状态
//        ProgressHUD.showOnView(view)
        
        //显示图片
        createScrollView()
        
    }
    
    //显示图片
    func createScrollView()
    {
        scrollView = UIScrollView(frame: CGRectMake(0,200,kScreenWidtn ,300))
        scrollView?.pagingEnabled = true
        scrollView?.delegate = self
        self.view.addSubview(scrollView!)
        
        let count = photoArray?.count
        let W = scrollView?.bounds.size.width
        let H = scrollView?.bounds.size.height
        
        for i in 0..<count!
        {
            let photoModel = photoArray![i]
            
            //图片
            let imageVIew = UIImageView(frame: CGRectMake(kScreenWidtn*CGFloat(i), 0, W!, H!))
            let url = NSURL(string: photoModel.originalUrl!)
            
            imageVIew.kf_setImageWithURL(url!)
          
            
            scrollView?.addSubview(imageVIew)
        }
        scrollView?.contentSize = CGSizeMake(W!*CGFloat(count!), 0)
        scrollView?.contentOffset = CGPointMake(W!*CGFloat(photoindex!), 0)
    }
    
    
    //自己加的类似导航的视图
    func createMyNav()
    {
        //背景图片
        let bgImageView = UIImageView(frame: CGRectMake(0, 20, kScreenWidtn, 44))
        bgImageView.userInteractionEnabled = true
        bgImageView.image = UIImage(named: "navigationbar")
        self.view.addSubview(bgImageView)
    
        //文字
        let title = "第\(photoindex! + 1)页,共\((photoArray?.count)!)页"
        titleLabel = UILabel.createlabelFrame(CGRectMake(80, 0, 215, 44), title: title, textAlignment: .Center)
        bgImageView.addSubview(titleLabel!)
        
        //按钮
        let button = UIButton.createBtn(CGRectMake(290, 4, 60, 36), title: "Done", bgImageName: "buttonbar_action", target: self, action: "doneAction")
        bgImageView.addSubview(button)
    }
    
    func doneAction()
    {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    
    //、修改当前视图控制器的状态栏样式
    override func prefersStatusBarHidden() -> Bool {
        return false
    }
    
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
     
        return UIStatusBarStyle.LightContent
    }
}
extension PhotoViewController : UIScrollViewDelegate
{
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
        
        let title = "第\(index + 1)页,共\((photoArray?.count)!)页"
        titleLabel?.text = title
        
    }
}

